<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
		<title>讨论章节列表页</title>
		<link rel="stylesheet" type="text/css" href="../../css/mui.min.css">
		<link rel="stylesheet" type="text/css" href="../../css/weui.min.css">
		<link rel="stylesheet" type="text/css" href="../../css/discuss.css">
		<link rel="stylesheet" href="../../css/CFNetPP_Base.css">
		<link rel="stylesheet" href="../../css/base.css">
		<style>
			html, body {
				background-color: #f0f0f0;
				padding: 0;
				margin: 0;
				font-family: "微软雅黑";
			}
			.weui_mask {
				position: fixed;
				z-index: 998;
				top: 0;
				right: 0;
				bottom: 0;
				left: 0;
			}
			.weui_dialog {
				z-index: 999;
			}
			.myScore2 {
				background: #fff;
				padding: 20px;
				border-bottom: 1px solid #d9d9d9;
				border-top: 1px solid #d9d9d9;
				/* margin-top: 10px; */
			}
		</style>
	</head>
	<body>
		<div id="main_div ">
			<div class=" writeReport " >
				<div class="bgwhite  "  >
					<div class="padding10" id="bewriteDIV" hidden="hidden" >
						<p class=" base_black_grey base_title_font">
							描述
						</p>
						<div class="base_dark_grey base_content_font according-2-rows text_indent_2 word-break" id="bewrite" onclick="openBewrite()"></div>
					</div>
					<div class="div_divider"></div>
					<div style="background-color: #f0f0f0;height:15px"></div>
					<div class="myScore2  sf_peix">
						<span class="font16 base_dark_grey "style='color: red'id="num">0<span class="font16 base_dark_grey left_5">条</span></span>
						<span class="font16 base_dark_grey left_10" id="theme">讨论</span>
					</div>
				</div>
				<div id="commentList">
					<ul class="mui-table-view "id="list_discus">
						<!--<li dataid="' + index + '" class="mui-table-view-cell mui-media list_item">
						<div onclick="openReplayWin(' + index + ')">
						<img class="mui-media-object mui-pull-left cell_user_icon" src="' + imgStr + '" style="display: block">
						<p class=" base_content_font base_dark_grey" >
						<span> 老网</span>
						<span class="left_5 font10">'
						刚刚 </span>
						</p>
						<div class="Commentbody top_2 ">
						<p class="word-break font12">
						地对地导弹
						</p>
						<p class="word-break left_50">
						地对地导弹
						</p>
						</div>
						</div>
						<table class="MBToolBar top_5 ">
						<tr>
						<td class="Comment" id="Reply" onclick="ReplayDiscus(' + index + ')"><img  src="../../image/icon_huifu.png"  class="incon_samll"><span class="left_5">4</span></td>
						</tr>
						</table>
						</li>-->
					</ul>
					<div class="noSign" hidden="hidden" id="noData">
						<img src="../../image/icon_img.png" width="60%">
						<p class="font16 top_15 base_dark_grey" >
							暂无讨论
						</p>
					</div>
					<div class="loadFailure" hidden="hidden" id="noNet">
						<img src="../../image/icon_img01.png" width="60%">
						<p class="" >
							数据加载失败
						</p>
						<p>
							请检查您的网络
						</p>
						<button type="button" class="mui-btn mui-btn-outlined" onclick="RefreshData()">
							重新加载
						</button>
					</div>
				</div>
				<div id="offlineAlert" class="weui_dialog_confirm" style="display: none;">
					<div class="weui_mask"></div>
					<div class="weui_dialog">
						<div class="weui_dialog_hd">
							<strong class="weui_dialog_title">确定删除此评论？</strong>
						</div>
						<div class="weui_dialog_bd"></div>
						<div class="weui_dialog_ft">
							<a onclick="cleanCancel()" href="#" class="weui_btn_dialog default"  style="color:#18b4ed">取消</a>
							<a onclick="cleanConfirm()" href="#" class="weui_btn_dialog primary"  style="color:#18b4ed">确定</a>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../../script/jquery.min.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script src="../../script/jquery.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="../../script/MD5.js"></script>
	<script src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript" src="../../script/push.js"></script>
	<script type="text/javascript">
		var page = 1;
		var row = 10;
		var refreshCount = 0;
		//更新数据条数
		var dataArr = new Array();
		var pid = "";
		// 计划id
		var cid = "";
		//课程id
		var chapterid = "";
		//章节id
		var title = "";
		var is_dis_bewrite = '';
		//被点击的题目id
		var Itemid = "";
		var dataLen = 0;
		apiready = function() {
			var pageParam = api.pageParam;
			pid = pageParam.pid;
			cid = pageParam.cid;
			chapterid = pageParam.chapterid;
			title = pageParam.cdetailtitle;
			bewrite();
			//接收通知
			ReceiveNotifi();
			//开启加载框
			showProgress();
			//加载数据
			loadNewData();
			//				SuspendedBall();
			//下拉加载
			setRefreshHeader();
			//上拉刷新
			setPullUpRefresh();
			//开启自动刷新
			// startDiscussUpdate();
		};
		function bewrite() {
			pageParam = api.pageParam;
			$('#nav_title').html(pageParam.title);
			var bewrite = pageParam.bewrite;
			if (bewrite != "" && bewrite != null && bewrite != undefined) {
				$('#bewriteDIV').show();
				if (removeHTMLTag(bewrite).length > 0) {
					$('#bewrite').html(removeHTMLTag(bewrite));
				} else {
					$('#bewrite').text('点击查看详情');
				}
			}
		}

		function openBewrite() {
			api.openWin({useWKWebView:true,
				name : 'Student_Free_bewrite',
				url : '../Study/Student_Free_bewrite.html',
				pageParam : {
					cdetailtitle : api.pageParam.cdetailtitle,
					bewrite : api.pageParam.bewrite,
					is_dis_bewrite : api.pageParam.is_dis_bewrite
				}
			});
		}

		function backyes() {
			$('#backAlert').hide();
			api.closeWin({
			});
		}

		function backCancel() {
			$('#backAlert').hide()
		}

		//接收通知刷新列表
		function ReceiveNotifi() {
			//编辑讨论的通知
			api.addEventListener({
				name : 'RefreshDiscusListNotifi'
			}, function(ret, err) {
				loadNewData();
			});
			//添加新的讨论的通知
			//			api.addEventListener({
			//				name : 'RefreshRepalyCommentNotifi'
			//			}, function(ret, err) {
			//				loadNewData();
			//			});
			api.addEventListener({
				name : 'RefreshAddDiscusListNotifi'
			}, function(ret, err) {
				loadNewData();
			});
			api.addEventListener({
				name : 'discussUpdateNotifi'
			}, function(ret, err) {
				loadNewData();
			});
			//添加总结刷新
			api.addEventListener({
				name : 'RefreshSummaryListNotifi'
			}, function(ret, err) {
				loadNewData();
			});
		}

		function loadNewData() {
			page = 1;
			loadData();
			//停止刷新控件
		}

		//		接收通知
		function loadMoreData() {
			page++;
			loadData();
		}

		function SuspendedBall() {
			api.openFrame({
				name : 'SuspendedBall',
				url : 'SuspendedBall.html',
				rect : {
					x : api.winWidth - 50,
					y : api.winHeight - 100,
					w : 40,
					h : 40,
				},
				pageParam : {
				},
				//				bounces : false,
				//				vScrollBarEnabled : true,
				//				hScrollBarEnabled : false,
				//				scrollEnabled : false,
			});
		}

		function saveTimeStamp() {
			var timestamp = new Date().getTime();
			var timeStampStr = timestamp.toString();
			$api.setStorage('discussLastTimeStamp', timeStampStr);
			$api.setStorage('discussLastCourseId', api.pageParam.cid);
			$api.setStorage('discussLastVid', api.pageParam.chapterid);
			$api.setStorage('discussLastPlanId', api.pageParam.pid);
			console.log("11111111");
		}

		function loadData() {
			var param = {
				pid : api.pageParam.pid,
				cid : api.pageParam.cid,
				vid : api.pageParam.chapterid,
				jid : $api.getStorage('cfnetppjxid'),
				uid : $api.getStorage('cfnetppuid'),
				page : page,
				rows : row,
			};
			consoleTest("url_discussList param : " + JSON.stringify(param));
			cfnetppPOST(url_discussList, param, true, function(data, status) {
				//			alert(JSON.stringify(param))
				//							alert(JSON.stringify(data))
				api.refreshHeaderLoadDone();
				hideProgress();
				if (status == 'success') {
					if (page == 1) {
						//存储最后刷新的一次时间戳
						saveTimeStamp();
					}
					$('#noNet').hide();
					if (data.code == 200) {
						$('#noData').hide();
						var list = data.json.list;
						dataLen = data.json.total;
						$('#num').text(dataLen);
						if (list != null && list.length > 0) {
							refreshCount = list.length;
							var listArr = new Array();
							var htmlString = new String();
							if (page == 1) {
								for (var i = 0; i < list.length; i++) {
									var weeklyData = list[i];
									var data = {
										uid : weeklyData.uid,
										id : weeklyData.id,
										userimg : weeklyData.picurl,
										username : weeklyData.rename,
										content : weeklyData.content,
										title : weeklyData.title,
										num : weeklyData.num,
										ctime : weeklyData.ctime,
										group_id : weeklyData.group_id,
										group_name : weeklyData.group_name
									};
									listArr.push(data);
									htmlString += htmlStringWithName(data, i);
								}
								dataArr = listArr;
								$('#list_discus').html(htmlString);
							} else {
								var oldCount = dataArr.length;
								for (var i = 0; i < list.length; i++) {
									var index = oldCount + i;
									var weeklyData = list[i];
									var data = {
										uid : weeklyData.uid,
										id : weeklyData.id,
										userimg : weeklyData.picurl,
										username : weeklyData.rename,
										content : weeklyData.content,
										title : weeklyData.title,
										num : weeklyData.num,
										ctime : weeklyData.ctime,
										group_id : weeklyData.group_id,
										group_name : weeklyData.group_name
									};
									listArr.push(data);
									htmlString += htmlStringWithName(data, index);
								}
								dataArr = dataArr.concat(listArr);
								$('#list_discus').append(htmlString);
							}
						} else {
							if (page == 1) {
								$('#noData').show();
							} else {
								api.toast({
									msg : '暂无数据',
									duration : 2000,
									location : 'bottom'
								});
							}
							$('#list_discus').html('');
						}
					} else if (data.code == 108) {
						if (page == 1) {
							$('#num').text('0');
							$('#list_discus').html('');
							$('#noData').show();
						}
						//
					} else {
						if (page == 1) {
							$('#list_discus').html('');
							$('#noData').show();
						}
					}
				} else {
					$('#noNet').show();
				}
			});
		}

		function htmlStringWithName(list, index) {
			var spanStr = '';
			var imgStr = '';
			if (list.userimg == null || list.userimg == '') {
				imgStr = '../../icon/icon_default_HeadPortrait.png';
			} else {
				imgStr = list.userimg;
			}
			if (list.uid == $api.getStorage('cfnetppuid')) {
				spanStr = '<td class="Retweet" onclick="EditDiscus(' + index + ')"><img class="incon_samll" src="../../image/icon_bianji.png"/> <span class="left_5">编辑</span></td>' + '<td class="Retweet" onclick="showAlert(' + list.id + ')"><img class="incon_samll" src="../../image/icon_shanchu.png"/ ><span class="left_5">删除</span></td>' + '<td class="Retweet" id="Reply" ><img  src="../../image/icon_huifu.png"  class="incon_samll"><span class="left_5">' + list.num + '</span></td>';
			} else {
				spanStr = '<td class="Comment" id="Reply" ><img  src="../../image/icon_huifu.png"  class="incon_samll"><span class="left_5">' + list.num + '</span></td>';
			}
			return '<li dataid="' + index + '" class="mui-table-view-cell mui-media list_item" onclick="openReplayWin(' + index + ')">' + '<div >' + '<img class="mui-media-object mui-pull-left cell_user_icon" src="' + imgStr + '" style="display: block">' + '<p class=" base_content_font base_dark_grey" >' + '<span>' + list.username + '</span>' + '<span class="left_5 font10">' + timestampFormat(list.ctime) + '</span>' + '</p>' + '<div class="Commentbody top_2 ">' + '<p class="word-break left_50 font12">' + groupidStr(list.group_id, list.group_name) + '</p>' + '<p class="word-break left_50">' + htmlspecialchars_decode(list.content) + '</p>' + '</div>' + '</div>' + '<table class="MBToolBar top_5 ">' + '<tr>' + spanStr + '</tr>' + '</table>' + '</li>';
		}

		function groupidStr(id, group_name) {
			if ( id = '' || id == 0 || id == '0' || id == null || id == 'null') {
				return '来自个人'
			} else {
				return '来自' + group_name + '小组'
			}
		}

		function showAlert(id) {
			event.stopPropagation();
			$('#offlineAlert').show();
			Itemid = id;
		}

		function cleanCancel() {
			$('#offlineAlert').hide();
		}

		//确定
		function cleanConfirm() {
			$('#offlineAlert').hide();
			deleteDiscus(Itemid);
			Itemid = "";
		}

		//删除评论
		function deleteDiscus(id) {
			var param = {
				id : id,
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjxid'),
				pid : api.pageParam.pid,
				cid : api.pageParam.cid,
				vid : api.pageParam.chapterid,
			};
			cfnetppPOST(url_deleteDiscuss, param, true, function(data, status) {
				hideProgress();
				if($api.getStorage('cfnetSelectType') == 0){//学生端
				  SendBusinessOperationsLogNotifi('自由课程讨论列表页', '032', param, param, '', data, '自由课程讨论章节学生删除讨论', '032-03')
				}else if($api.getStorage('cfnetSelectType') == 1){//老师端
				  SendBusinessOperationsLogNotifi('自由课程讨论列表页', '032', param, param, '', data, '自由课程讨论章节老师删除讨论', '032-08')
				}
				if (status == 'success') {
					if (data.code == 200) {
						ShowToast('删除成功');
						Itemid = "";
						loadNewData();
					} else {
						api.toast({
							msg : '删除失败',
							duration : 2000,
							location : 'bottom'
						});
					}
				} else {
					api.toast({
						msg : '获取数据失败',
						duration : 2000,
						location : 'bottom'
					});
				}
			});
		}

		//编辑评论
		function EditDiscus(index) {
			event.stopPropagation();
			var data = dataArr[parseInt(index)];
			var id = data.id;
			var content = data.content;
			api.openFrame({
				name : 'New_FreeCourse_EditDiscus',
				url : 'New_FreeCourse_EditDiscus.html',
				pageParam : {
					id : id,
					pid : api.pageParam.pid,
					title : data.title,
					chapterid : api.pageParam.chapterid,
					content : content
				}
				//
			});
		}

		//回复讨论
		function ReplayDiscus(index) {
			//			var data = dataArr[parseInt(index)];
			//			var id = data.id;
			//			api.openFrame({
			//				name : 'New_Study_ReplayDiscus',
			//				url : 'New_Study_ReplayDiscus.html',
			//				pageParam : {
			//					id : id,
			//					pid : pid,
			//					title : title,
			//					cid : cid,
			//					chapterid : chapterid,
			//				}
			//				//
			//			});
		}

		//列表条目点击
		function openReplayWin(index) {
			event.stopPropagation();
			var data = dataArr[parseInt(index)];
			var id = data.id;
			var userimg = data.userimg;
			api.openWin({useWKWebView:true,
				name : 'New_Student_Study_DiscussChapterReplayWindow',
				url : 'New_Student_Study_DiscussChapterReplayWindow.html',
				pageParam : {
					data : data,
					title : api.pageParam.cdetailtitle,
					pid : api.pageParam.pid,
					cid : api.pageParam.cid,
					chapterid : api.pageParam.chapterid,
					type : 0,
					sourceurl : window.location.pathname,
					sourcetitle : document.title,
				}
			});
		}
	</script>
</html>